﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DTO;
namespace DAL
{
    public class BO_DEMDAL:KetNoiDuLieu
    {
        public void Insert(BO_DEM bd)
        {
            SqlCommand cm = new SqlCommand("sp_bodem_Insert", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TRINH_DUYET", bd.Trinhduyet);
            cm.Parameters.AddWithValue("@IP", bd.IP);
            cm.Parameters.AddWithValue("@THOI_GIAN", bd.Thoigian);
            cm.Parameters.AddWithValue("@LINK", bd.Link);
            cm.ExecuteNonQuery();
        }
        public void Update(BO_DEM bd)
        {
            SqlCommand cm = new SqlCommand("sp_bodem_Update", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA", bd.Ma);
            cm.Parameters.AddWithValue("@TRINH_DUYET",bd.Trinhduyet );
            cm.Parameters.AddWithValue("@IP", bd.IP);
            cm.Parameters.AddWithValue("@THOI_GIAN", bd.Thoigian);
            cm.Parameters.AddWithValue("@LINK", bd.Link);
            cm.ExecuteNonQuery();
        }
        public void Delete(BO_DEM Ma)
        {
            SqlCommand cm = new SqlCommand("sp_bodem_Delete", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA", Ma);
            cm.ExecuteNonQuery();
        }
        public Bo_demColection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                        string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_bodem_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            Bo_demColection bdcl = new Bo_demColection();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                BO_DEM bd = new BO_DEM();
                bd.Ma = int.Parse(ds.Tables[0].Rows[i]["MA"].ToString());
                bd.Trinhduyet = ds.Tables[0].Rows[i]["TRINH_DUYET"].ToString();
                bd.IP = ds.Tables[0].Rows[i]["IP"].ToString();
                bd.Thoigian = DateTime.Parse(ds.Tables[0].Rows[i]["THOI_GIAN"].ToString());
                bd.Link = ds.Tables[0].Rows[i]["LINK"].ToString();
                bdcl.Add(bd);

            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return bdcl;
        }

        public int so_luong_hom_nay(string from, string to)
        {
            SqlCommand cm = new SqlCommand("select count(*) as number from bo_dem where '" + from + "' <= thoi_gian and thoi_gian <= '" + to + "'", LayKetNoi());
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return int.Parse(ds.Tables[0].Rows[0]["number"].ToString());
        }


        public void Delete(int Ma)
        {
            throw new NotImplementedException();
        }
    }
}
